gcapp.set "messageVisible","false"

gcapp.get doc,frontDocument
doc.get parent,"parentStructure"

! look for "Copy Source" of a source record
if @parent.level=0
  ! can't use && because recordType only allowed for records
  if @parent.recordType="SOUR"
    ! create gedcomArray {{1 SOUR ID}}
    CreateList slink,1,"SOUR",@parent.id
    CreateList garray,slink
    CreateList scopy,"Sources",garray
    scopy.putPasteboard
    exit
  endif
endif

! find source subordinate to parent only
parent.findStructures sources,"SOUR"

! this limits to only subordinate sources
! to get source at any level set #maxLevel to 99
#maxLevel=@parent.level+1

! First list will be to put on pasteboard
! second has names in case user needs to select them
doc.get sourRecs,"sources"
CreateList copySource,"Sources"
CreateList copyNames
Repeat "#i",0,@sources.count-1
  sources.#i.get slink
  if @slink.level<=#maxLevel
    copySource.addObject slink
    #sid=@slink.contents
    sourRecs.#sid.get srec
    copyNames.addString @srec.sourceTitle
  endif
EndRepeat

! should never happen if browser style only adds "Copy Source"
!     button when sources are available
#locOK=local("OK")
#locCancel=local("Cancel")
if @copySource.count<2
  beep
  #msg=local("No sources found at this location")
  UserOption "#var",#msg,"",#locOK
  exit
endif

! If only one source, put on pasteboard
if @copySource.count=2
  copySource.putPasteboard
  ReadPasteboard pb
  exit
endif

! when multiple source, users can select any number
#promp=local("Choose one or more sources to copy")
UserChoice "#var",#promp,copyNames,local("Sources"),"true",#locOK,#locCancel
if #var[1]=#locCancel
  exit
endif

! first selection at n+2 (#var[0]=2n+1)
#ist=(#var[0]+3)/2
CreateList copySource2,"Sources"
Repeat "#i",#ist,#var[0]
  #pick=#var[#i]
  copySource.#pick.get slink
  copySource2.addObject slink
EndRepeat
copySource2.putPasteboard
